package com.bootdo.app.service.impl;

import com.bootdo.app.htmlVO.index.Login;
import com.bootdo.app.service.AppIndexService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.ZimeBankUtil;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.dao.UseraccountDao;
import com.bootdo.appsystem.dao.UserdetailsDao;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.domain.UserdetailsDO;
import com.bootdo.common.utils.HttpContextUtils;
import com.bootdo.common.utils.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】
 * 【@Date   】 2018/5/12 0012
 **/
@Service
public class AppIndexServiceImpl implements AppIndexService {

    final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UseraccountDao useraccountDao;

    @Autowired
    private UserdetailsDao userdetailsDao;


    @Override
    public ServerResponse<String> msgLoginInfo(Login login) {
        HashMap<String , Object> map = new HashMap<>();
        if(login.getUsername() == null || login.getUsername().equals("")){
            return ServerResponse.createByErrorMessage("请输入用户名");
        }
        if(login.getPassword() == null || login.getPassword().equals("")){
            return ServerResponse.createByErrorMessage("请输入密码");
        }

        String encrypt = MD5Utils.encrypt(AppUtilXML.SCOKED_NUMBER, login.getPassword());
        login.setPassword(encrypt);

//        输入手机的会员进行登录验证
        if(ZimeBankUtil.isPhone(login.getUsername())){
            map.put("mobile",login.getUsername());
            List<UserdetailsDO> userdetailsDOList = userdetailsDao.list(map);
            if(userdetailsDOList.size() > 0){
                UseraccountDO userapp = useraccountDao.get(userdetailsDOList.get(0).getUid());
                return getUserLocked(userapp , login);
            }
        }
//        对用户名不是输入的手机号的会员进行验证登录
        try {
            Integer username = 0;
            username = ZimeBankUtil.isInteger(login.getUsername());
            if(username > 0){
                UseraccountDO userapp = useraccountDao.get(username);
                return getUserLocked(userapp , login);
            }
        }catch (RuntimeException e){
            return ServerResponse.createByErrorMessage("登录失败,请检查用户名");
        }
        return ServerResponse.createByErrorMessage("登录失败,请检查用户名或密码");
    }


    private ServerResponse getUserLocked(UseraccountDO userapp , Login login ){
        if(userapp != null) {
            if(userapp.getLocked() == -1){
                //锁定
                return ServerResponse.createByErrorCodeMessage(5000,"您的账号已被锁定！");
            }else if (userapp.getPassword().equals(login.getPassword())) {
                HttpContextUtils.getHttpServletRequest().getSession().setAttribute(AppUtilXML.SESSION_CUSTOMER, userapp);
                return ServerResponse.createBySuccess();
            }
        }
        return ServerResponse.createByErrorMessage("登录失败,请检查用户名或密码");
    }




}
